#include <types.h>
#include <osal/osal.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"

#include "osd_config.h"
#include "string.id"
#include "images.id"

#include "win_com.h"
#include "win_mute.h"


/*******************************************************************************
* WINDOW's objects declaration
*******************************************************************************/

extern BITMAP win_mute_bmp;

BOOL  cur_mute_state=UNMUTE_STATE;	
BOOL  pre_mute_state = UNMUTE_STATE;

/*******************************************************************************
* Objects definition
*******************************************************************************/

DEF_BITMAP(win_mute_bmp,NULL,NULL,C_ATTR_ACTIVE,0, \
    1,0,0,0,0, MUTE_L,MUTE_T,MUTE_W,MUTE_H, WSTL_TRANS_IX,WSTL_TRANS_IX,0,0,   \
    NULL,NULL,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,MUTE_ICON)


/*******************************************************************************
* Function definition
*******************************************************************************/

/***********************************************/

BOOL GetMuteState()
{
	return cur_mute_state;
}

void SaveMuteState()
{
	pre_mute_state = cur_mute_state;
}

BOOL MuteStateIsChange()
{
    return pre_mute_state != cur_mute_state;
}

static UINT8 m_mute_on_screen;

void ShowMuteOSDOnOff(UINT8 flag)
{
    OBJECT_HEAD* obj;
    obj = (OBJECT_HEAD*)&win_mute_bmp;

    if (flag != m_mute_on_screen)
    {
        if(flag)
            api_inc_wnd_count();
    }

#ifdef _INVW_JUICE //v0.1.4
	if (inview_is_started())
	{
    	if (!INVIEW_isAppBusy())
		{
			INVIEW_pause();
    	}
	}
#endif 
    if(flag)
        OSD_DrawObject(obj, C_UPDATE_ALL);
    else
        OSD_ClearObject(obj, 0);

    if (flag != m_mute_on_screen)
    {
        if(!flag)
            api_dec_wnd_count();
        m_mute_on_screen = flag;
    }

}

void ShowMuteOnOff(void)
{
    if(GetMuteState() && MuteStateIsChange() && TRUE == GetChannelParrentLock())
        return;
        ShowMuteOSDOnOff(cur_mute_state==MUTE_STATE);
}

void SetMuteOnOff(BOOL showOsd)
{
    SYSTEM_DATA* sys_data;
    sys_data = sys_data_get();    

    if(cur_mute_state==UNMUTE_STATE)
        cur_mute_state = MUTE_STATE;
    else
        cur_mute_state = UNMUTE_STATE;

#ifdef AUDIO_MUTE_SAVE
    sys_data->bMuteSate = cur_mute_state;
#endif

#ifdef SYS_VOLUME_FOR_PER_PROGRAM
	if(cur_mute_state == UNMUTE_STATE)
	{
		UINT8 volume;
		if(get_channel_vol(&volume))
			api_audio_set_volume(volume);
	}
#endif	
    api_audio_set_mute(cur_mute_state);

    if(showOsd)
        ShowMuteOnOff();
}

void GetMuteRect(OSD_RECT* rect)
{
	POBJECT_HEAD obj;

	obj = (POBJECT_HEAD)&win_mute_bmp;
	*rect = obj->frame;
}

#ifdef _INVW_JUICE
void mute_switch_pause_process(BOOL en)
{

	if(!en || cur_mute_state==UNMUTE_STATE)
		SetMuteOnOff(FALSE);
	else if(cur_mute_state == MUTE_STATE)
	{
		cur_mute_state=UNMUTE_STATE;
			ShowMuteOnOff();
		cur_mute_state=MUTE_STATE;
	}
}
#endif
